<div id="header" class="l-f l-f-middle">
    <div id="headerLeft"><i class="iconfont">&#xf602;</i></div>
    <div id="headerTitle" class="l-f-g1 t-center">123</div>
    <div id="headerRight"></div>
    <script>
        $('#headerLeft i').on('click', function(){
            history.back();
        });
    </script>
</div>

<div id="bodyer">
    <div id="insideAd">
        <a href="#"><img src="/img/formBanner.png" alt=""/></a>
    </div>
    <div id="form">
        <form action="">
            <div class="box">
                <div class="item l-f l-f-middle">
                    <div class="title">公司名称</div>
                    <div class="cont l-f-g1">
                        <input type="text" id="companyName" name="Contactor[company_name]" class="ipt_txt" value=""/>
                    </div>
                    <div class="must">*</div>
                </div>
                <div class="item l-f l-f-middle">
                    <div class="title">公司所属地区</div>
                    <div class="cont l-f-g1">
                        <input id="companyArea" type="text" name="Contactor[city_id]" class="ipt_txt" placeholder="请选择。。。"/>
                    </div>
                    <div class="must">*</div>
                </div>
                <script>
                    var area = new com.Select('#companyArea');
                    area.debug = false;
                    area.type = 'city';
                    area.dataUrl = '/home/city';
                    area.init();

                </script>
                <div class="item l-f l-f-middle">
                    <div class="title">联系人</div>
                    <div class="cont l-f-g1">
                        <input type="text" id="name" name="Contactor[name]" class="ipt_txt" value=""/>
                    </div>
                    <div class="must">*</div>
                </div>
                <div class="item l-f l-f-middle">
                    <div class="title">职务</div>
                    <div class="cont l-f-g1">
                        <input type="text" name="Contactor[position]" class="ipt_txt"/>
                    </div>
                    <div class="must"></div>
                </div>
                <div class="item l-f l-f-middle">
                    <div class="title">手机号码</div>
                    <div class="cont l-f-g1">
                        <input type="tel" id="tel" name="Contactor[mobile]" class="ipt_txt"/>
                    </div>
                    <div class="must">*</div>
                </div>
                <div class="item l-f l-f-middle">
                    <div class="title">主营产品</div>
                    <div class="cont l-f-g1">
                        <input id="selectCategory" type="text" name="cat" class="ipt_txt" placeholder="请选择。。。"/>
                    </div>
                    <div class="must"></div>
                </div>
                <script>
                    var category = new com.Select('#selectCategory');
                    category.debug = false;
                    category.level = [1, 2];
                    category.dataUrl = '/home/cat';
                    category.multiple = 99;
                    category.init();
                </script>
                <div class="item l-f l-f-middle">
                    <div class="title">公司网址</div>
                    <div class="cont l-f-g1">
                        <input type="text" name="Contactor[web_site]" class="ipt_txt"/>
                    </div>
                    <div class="must"></div>
                </div>
                <div class="item l-f l-f-middle">
                    <div class="title">微信号</div>
                    <div class="cont l-f-g1">
                        <input type="text" name="Contactor[wechat]" class="ipt_txt"/>
                    </div>
                    <div class="must"></div>
                </div>
            </div>
            <div class="btn l-f l-f-middle">
                <div class="l-f-g1">
                    <input type="submit" value="确定提交"/>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    var checkTelUrl = '/home/check-mobile'; //异步检测手机号重复 get
    var postUrl = '';   //异步提交 post
    //ajax返回 {success: 成功状态, msg: 提示信息 }

    $('#form form').on('submit', function(){
        var error = [];
        var companyName = $.trim($('#companyName').val());
        var companyArea = $.trim($('#companyArea').val());
        var name = $.trim($('#name').val());
        var tel = $.trim($('#tel').val());

        //基本验证
        if($.trim(companyName) == ''){
            error.push('请填写您的公司名称！');
        }
        if($.trim(companyArea) == ''){
            error.push('请选择所在地区！');
        }
        if($.trim(name) == ''){
            error.push('请填写联系人姓名！');
        }
        if($.trim(tel) == ''){
            error.push('请填写您的手机！');
        }
        if(error.length > 0){
            com.toast(error.join('<br/>'));
            return false;
        }

        //手机号验证
        var handPhone = [                //手机号码段
            '130', '131', '132', '133', '134', '135', '136', '137', '138', '139',
            '145', '147',
            '150', '151', '152', '153', '155', '156', '157', '158', '159',
            '170', '176', '177', '178',
            '180', '181', '182', '183', '184', '185', '186', '187', '188', '189'
        ];
        var reg = '^(' + handPhone.join('|') + ')\\d{8}$';
        if(!new RegExp(reg).test(tel)){
            com.toast('手机号码格式不正确！');
            return false;
        }

        //手机号重复
        $.get(checkTelUrl, {tel: tel}, function(data){
            if(data.success){
                submit();
            } else {
                com.confirm(data.msg, function(layerId){
                    layer.close(layerId);
                    submit();
                }, function(){});
            }
        }, 'json');
        return false;
    });

    var submit = function(){
        $.post(postUrl, $('#form form').serializeArray(), function(data){
            com.toast(data.msg);
            if(data.success){
                window.location.href = '/home/view';
            }
        }, 'json');
    }
</script>

<?=$this->render('_foot');?>
</body>